limsLocation.editPage = {

    getBxForm: function () {
        var _this = this;
        return {
            type: 'form',
            columns: 1,
            id: 'location-select-form',
            titleWidthAuto: true,
            fields: limsLocation.getSelectInfoFormFields(),
        }
    },

    refreshSelectForm: function () {
        let url = Gikam.select.getConfigParam("refrigeratorChoose").url + Gikam.param({
            parentId_EQ: 1
        });
        Gikam.getJsonSync(url).done(function (data) {
            let selectData = [];
            selectData.push({
                value: '',
                text: ''
            })
            if (!Gikam.isEmpty(data)) {
                Gikam.each(data, function (index, item) {
                    selectData.push({
                        value: item.id,
                        text: item.locationDesc
                    })
                });
            }
            Gikam.getComp('location-select-form').setSelectOptions('locationDesc', selectData);
        });
    },

    getBtnToolbarParam: function () {
        var _this = this;
        return {
            type: 'btnGroup',
            items: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                onClick: function () {
                    let data = Gikam.getComp('location-select-form').getData();
                    Gikam.preInsert({
                        modalTitle: 'GIKAM.BUTTON.INSERT',
                        fields: limsLocation.getBaseInfoFormFields(data.storage),
                        disableNotEmptyValidate: true,
                        url: limsLocation.baseUrl
                    }).done(function (id) {
                        Gikam.getComp('lims-location-tree-grid').refresh();
                        _this.refreshSelectForm();
                    });
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                onClick: function () {
                    let tree = Gikam.getComp('lims-location-tree-grid');
                    let selectedNode = tree.getSelectedNode();
                    if (Gikam.isEmpty(selectedNode)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                        return;
                    }
                    let node = selectedNode.locationDesc;
                    Gikam.confirm('GIKAM.BUTTON.CONFIRM', Gikam.propI18N("LIMS.LOCATION.BUTTON.DELETE_LOCATION") + ":<br>" + node, function () {
                        workspace.window.showMask();
                        let url;
                        if (selectedNode.id === selectedNode.group) {
                            url = limsLocation.baseUrl + '/delete/' + selectedNode.group;
                            Gikam.del(url).done(function () {
                                _this.refreshSelectForm();
                                tree.refresh();
                            }).always(function () {
                                workspace.window.closeMask();
                            })
                        } else {
                            // 不改变原有删除冰箱的逻辑，增加删除冰箱节点下的层级功能
                            let selections = [];
                            if (selectedNode.childQty > 0) {
                                let treeList = tree.getTreeList();
                                let bool = treeList.filter(t => t.id === selectedNode.group);
                                if (bool.length === 0) {
                                    workspace.window.closeMask();
                                    return;
                                }
                                // 当前根节点
                                let node = bool[0];
                                // 获取节点下所有id
                                let currentNode;
                                if (node.childQty > 0 && !Gikam.isEmpty(node.children)) {
                                    currentNode = limsLocation.getCurrentNode(node.children, selectedNode);
                                }
                                if (Gikam.isEmpty(currentNode)) {
                                    currentNode = selectedNode;
                                }
                                selections.push({
                                    id : selectedNode.id
                                });
                                if (currentNode.childQty > 0 && !Gikam.isEmpty(currentNode.children)) {
                                    limsLocation.getSelectionsByNode(currentNode.children, selections);
                                }
                            } else {
                                selections.push({
                                    id : selectedNode.id
                                })
                            }

                            // 根据id删除
                            Gikam.del(limsLocation.baseUrl, Gikam.getJsonWrapper(null, ['', selections])).done(function () {
                                _this.refreshSelectForm();
                                tree.refresh();
                            }).always(function () {
                                workspace.window.closeMask();
                            })
                        }

                    })
                }
            }, {
                type: 'button',
                text: 'T_LIMS_LOCATION.BUTTON.ADD_APPEND',
                icon: 'add-row',
                onClick: function () {
                    let tree = Gikam.getComp('lims-location-tree-grid');
                    // 当前选中的节点
                    let selectedNode = tree.getSelectedNode();
                    if (Gikam.isEmpty(selectedNode)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                        return;
                    }
                    let treeList = tree.getTreeList();
                    let bool = treeList.filter(t => t.id === selectedNode.group);
                    if (bool.length === 0) return;
                    // 当前根节点
                    let node = bool[0];
                    // 删除属性，防止覆盖
                    delete selectedNode.ext$;
                    Gikam.preInsert({
                        modalTitle: 'CORE.MODULE.ITEM.NOTICES.MODAL.ADD',
                        fields: limsLocation.getAddInfoFormFields(node, selectedNode),
                        disableNotEmptyValidate: true,
                        displayReadonly: false,
                        url: limsLocation.baseUrl + '/append/locations',
                        param: selectedNode
                    }).done(function (id) {
                        if (!Gikam.isEmpty(id)) {
                            tree.refresh();
                            _this.refreshSelectForm();
                        }
                    });
                }
            }]
        }
    },

    getTreeGrid: function () {
        var _this = this;
        return {
            type: 'tree',
            id: 'lims-location-tree-grid',
            url: limsLocation.baseUrl + '/tree/1',
            nodeTextField: 'locationDesc',
            onNodeSelected: function (nodeId, node) {
                let tab = Gikam.getComp('location-tab');
                if (node.level === '4') {
                    tab.removePanel('location-panel');
                    tab.revertPanel('board-panel');
                    Gikam.getAsyncComp('lims-location-board-edit-list-grid').done(function (grid) {
                        grid.refresh({
                            url: limsLocation.boardUrl + '/queries',
                            requestData: {
                                locationNo_CISB: node.locationNo,
                                createdByOrgId_EQ: workspace.user.ext$.loginorgid,
                            }
                        });
                    });

                    Gikam.getAsyncComp('location-sample-info-list-grid').done(function (grid) {
                        grid.refresh({
                            url: limsLocation.sampleUrl + '/queries',
                            requestData: {
                                locationId_EQ: node.id
                            }
                        });
                    });
                } else {
                    tab.revertPanel('location-panel');
                    tab.removePanel('board-panel');
                    Gikam.getAsyncComp('location-edit-list-grid').done(function (grid) {
                        grid.refresh({
                            url: limsLocation.baseUrl + '/queries',
                            requestData: {
                                parentId_EQ: node.id,
                                createdByOrgId_EQ: workspace.user.ext$.loginorgid,
                            }
                        });
                    });
                }
            },
            requestData: {
                createdByOrgId_EQ: workspace.user.ext$.loginorgid,
            },
            onLoadSuccess: function (data) {
                if (data.length <= 0) {
                    Gikam.getAsyncComp('location-edit-list-grid').done(grid => {
                        grid.cleanData()
                    });
                    Gikam.getAsyncComp('location-sample-info-list-grid').done(grid => {
                        grid.cleanData()
                    })
                }
            },
        }
    },

    getGridParam: function () {
        var _this = this;
        return {
            type: 'grid',
            id: 'location-edit-list-grid',
            service: 'limsLocationServiceImpl',
            url: limsLocation.baseUrl + '/queries',
            requestData: {
                parentId_EQ: 1,
                createdByOrgId_EQ: workspace.user.ext$.loginorgid,
            },
            columns: limsLocation.getEditListColumns(),
            onUpdated: function (data) {
                if (!Gikam.isEmpty(data.locationDesc)) {
                    Gikam.getComp('lims-location-tree-grid').refresh();
                }
            },
        }
    },

    getBoardTab: function () {
        var _this = this;
        return {
            type: 'grid',
            id: 'lims-location-board-edit-list-grid',
            service: 'limsLocationBoardServiceImpl',
            dbTable: 'T_LIMS_LOCATION_BOARD',
            columns: limsLocation.getBoardListColumns(),
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0);
                } else {
                    Gikam.getComp('sample-Location').rowNum = 0;
                    Gikam.getComp('sample-Location').setData([]);
                }
            },
            onRowActive: function (index, row) {
                Gikam.postSync(limsLocation.holeUrl + '/queries', Gikam.getJsonWrapper({
                    f: {
                        boardId_EQ: row.id
                    },
                    n: -1, s: -1
                })).done(function (data) {
                    if (Gikam.isNotEmpty(data.rows)) {
                        Gikam.getComp('sample-Location').setData(data.rows);
                    }
                });
            },
            toolbar: [{
                type: 'button',
                text: 'T_LIMS_LOCATION.BUTTON.ADD_BOARD',
                icon: 'add',
                onClick: function () {
                    let selectedNode = Gikam.getComp('lims-location-tree-grid').getSelectedNode();
                    if (Gikam.isEmpty(selectedNode)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                        return;
                    }
                    Gikam.create('modal', {
                        width: '80%',
                        height: '80%',
                        title: Gikam.choose.getConfig("board-choose").title,
                        url: IFM_CONTEXT + Gikam.choose.getConfig("board-choose").url + Gikam.param({
                            locationId: 'null',
                            type: 'board'
                        }),
                        onAfterClose: function (rows) {
                            if (!Gikam.isEmpty(rows)) {
                                var chooseData = rows.map(function (item) {
                                    return {
                                        id: item.id,
                                        locationId: selectedNode.id,
                                        locationNo: selectedNode.locationNo
                                    }
                                });
                                Gikam.postText(limsLocation.baseUrl + '/insert/boards', Gikam.getJsonWrapper({action : '1'}, [null, chooseData])).done(function () {
                                    Gikam.getComp('lims-location-board-edit-list-grid').refresh();
                                });
                            }
                        }
                    });
                }
            }, {
                type: 'button',
                text: 'T_LIMS_LOCATION.BUTTON.DELETE_BOARD',
                icon: 'remove',
                onClick: function () {
                    let selections = Gikam.getComp('lims-location-board-edit-list-grid').getSelections();
                    if (Gikam.isEmpty(selections)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                        return;
                    }
                    let selectedNode = Gikam.getComp('lims-location-tree-grid').getSelectedNode();
                    selections.map(selection => selection.boardLocationNo = selectedNode.locationNo);
                    Gikam.confirm('GIKAM.BUTTON.CONFIRM', 'T_LIMS_LOCATION.REMOVE_BOARD', function () {
                        workspace.window.showMask();
                        Gikam.postText(limsLocation.baseUrl + '/update/boards',
                            Gikam.getJsonWrapper(null, [null, selections])).done(function () {
                            Gikam.getComp('lims-location-board-edit-list-grid').refresh();
                        }).always(function () {
                            workspace.window.closeMask();
                        })
                    })
                }
            }, {
                type: 'button',
                text: 'T_LIMS_LOCATION.BUTTON.REMOVE_BOARD',
                icon: 'add',
                onClick: function () {
                    let selectedNode = Gikam.getComp('lims-location-tree-grid').getSelectedNode();
                    if (Gikam.isEmpty(selectedNode)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                        return;
                    }
                    Gikam.create('modal', {
                        width: '80%',
                        height: '80%',
                        title: Gikam.choose.getConfig("board-choose").title,
                        url: IFM_CONTEXT + Gikam.choose.getConfig("board-choose").url,
                        onAfterClose: function (rows) {
                            if (!Gikam.isEmpty(rows)) {
                                var chooseData = rows.map(function (item) {
                                    return {
                                        id: item.id,
                                        locationId: selectedNode.id,
                                        locationNo: selectedNode.locationNo,
                                        boardLocationNo : selectedNode.locationNo
                                    }
                                });
                                Gikam.postText(limsLocation.baseUrl + '/insert/boards', Gikam.getJsonWrapper({action : '2'}, [null, chooseData])).done(function () {
                                    Gikam.getComp('lims-location-board-edit-list-grid').refresh();
                                });
                            }
                        }
                    });
                }
            }]
        }
    },

    // 孔
    getHoleTab: function () {
        var _this = this;
        return {
            type: 'sampleLocation',
            id: 'sample-Location',
            bgIcon: {
                blood: '/static/lsi/img/location/blood.svg',
                cell: '/static/lsi/img/location/stem-cell.svg',
                dna: '/static/lsi/img/location/dna.svg'
            },
            bgColor: {
                in: 'green',
                out: 'blue',
            },
            rowNum: 0,
            colNum: 0,
            data: [],
            onClick: function (data) {
                let form = Gikam.getComp('location-hole-info-form');
                if (data.sampleId) {
                    form.refresh({
                        url: Gikam.printf(limsLocation.sampleUrl + '/{id}', {
                            id: data.sampleId
                        })
                    });
                } else {
                    form.cleanData();
                }
            },
            onDbclick: function (data) {
            },
            onRendered: function (data) {
            }
        }
    },

    // 样本信息
    getHoleInformTab: function () {
        var _this = this;
        return {
            id: 'location-hole-info-form',
            type: 'form',
            titleWidthAuto: true,
            readonly: true,
            fields: limsLocation.getHoleDetailInfoFormFields(),
            columns: 3,
        }
    },

    create: function () {
        var _this = this;
        Gikam.create('layout', {
            id: 'lims-location-edit-list-layout',
            renderTo: workspace.window.$dom,
            west: {
                width: 240,
                title: 'T_LOCATION.BX',
                items: [
                    {
                        type: 'layout',
                        north: {
                            height: 130,
                            id: 'location-btn-group',
                            items: [_this.getBtnToolbarParam(), _this.getBxForm()]
                        },
                        center: {
                            height: '87%',
                            items: [_this.getTreeGrid()]
                        },
                    }
                ]
            },
            center: {
                items: [{
                    type: 'tab',
                    id: 'location-tab',
                    panels: [{
                        id: 'location-panel',
                        title: 'T_LIMS_LOCATION.TITLE.LOCATION',
                        items: [_this.getGridParam()]
                    }, {
                        id: 'board-panel',
                        title: 'T_LIMS_LOCATION_BOARD.TITLE.BOARD',
                        items: [{
                            type: 'layout',
                            west: {
                                width: '40%',
                                items: [_this.getBoardTab()]
                            },
                            center: {
                                items: [{
                                    type: 'layout',
                                    center: {
                                        items: [_this.getHoleTab()]
                                    },
                                    south: {
                                        height: 100,
                                        title: 'MODULE.BUSINESS.ORDERTASK.TAB.SAMPLE_INFO',
                                        items: [_this.getHoleInformTab()]
                                    }
                                }]
                            }
                        }],
                    }],
                    onRendered: function () {
                        Gikam.getComp('location-tab').removePanel('board-panel');
                    }
                }]
            }
        });
    },

    init: function () {
        this.create();
    }
};
